package cc.rengu.igas.route.core.realize.payment.impl.nucc;

import cc.rengu.igas.channel.nucc.biz.impl.NuccAcqPayServiceImpl;
import cc.rengu.igas.channel.nucc.facade.base.Header;
import cc.rengu.igas.channel.nucc.facade.request.VerifySignRequest;
import cc.rengu.igas.channel.nucc.facade.response.VerifySignResponse;
import cc.rengu.igas.channel.nucc.facade.result.Result;
import cc.rengu.igas.route.common.enums.SmartRouteRspEnums;
import cc.rengu.igas.route.core.realize.payment.DstChannelPaymentSignVerifyService;
import cc.rengu.igas.route.facade.bean.PayerInfo;
import cc.rengu.igas.route.facade.request.IntelligencePaymentSignVerifyRequest;
import cc.rengu.igas.route.facade.response.IntelligencePaymentSignVerifyResponse;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 网联对外收款签约确认实现 协议签约
 *
 * @author lizhangtao
 * @version 1.0.0
 * @date 2020-04-30
 */
public class NuccPaymentSignVerifyServiceImpl implements DstChannelPaymentSignVerifyService {
    private final static String TXN_NUM = "T00NP101";

    @Override
    public IntelligencePaymentSignVerifyResponse paymentSignVerify(IntelligencePaymentSignVerifyRequest intelligencePaymentSignVerifyRequest, PayerInfo payerInfo, String origSysSeqNum) throws Exception {
        VerifySignRequest verifySignRequest = new VerifySignRequest();
        RgLogger rglog = RgLog.getLogger(this.getClass().getSimpleName());
        IntelligencePaymentSignVerifyResponse intelligencePaymentSignVerifyResponse = new IntelligencePaymentSignVerifyResponse();
        intelligencePaymentSignVerifyResponse.setHeader(intelligencePaymentSignVerifyRequest.getHeader());
        //设置请求头
        Header header = new Header();
        BeanUtil.beanCopy(intelligencePaymentSignVerifyRequest.getHeader(), header);
        header.setTxnNum(TXN_NUM);
        verifySignRequest.setHeader(header);
        String additionInfo = intelligencePaymentSignVerifyRequest.getAdditionInfo();
        if (StringUtil.isEmptyOrNull(additionInfo)) {
            rglog.error("无原始信息");
            throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
        } else {
            JSONObject jsonObject = JSON.parseObject(additionInfo);
            if (null == jsonObject) {
                rglog.error("无原始信息");
                throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
            //设置短信key
            String smsKey = jsonObject.getString("authMsgId");
            if (StringUtil.isEmptyOrNull(smsKey)) {
                rglog.error("无短信key[authMsgId]");
                throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
//            verifySignRequest.set(smsKey);
            //设置原通道交易日期时间
            String origDateTime = jsonObject.getString("origTrxDtTm");
            if (StringUtil.isEmptyOrNull(origDateTime)) {
                rglog.error("无原始通道流水号");
                throw new BizException(SmartRouteRspEnums.TRANS_FAIL.getRespCode(), SmartRouteRspEnums.TRANS_FAIL.getRespDesc());
            }
            verifySignRequest.setOriTrxDtTm(origDateTime);
        }
        //设置请求值
        verifySignRequest.setInstgId(intelligencePaymentSignVerifyRequest.getHeader().getInstId());
        verifySignRequest.setOriTrxDtTm("");
        verifySignRequest.setOriTrxId(intelligencePaymentSignVerifyRequest.getOrigOrder());
        //网联交易类型，区分签约与签约确认 0202 协议签约-签约
        verifySignRequest.setTrxCtgy("0202");
        verifySignRequest.setAuthMsg(intelligencePaymentSignVerifyRequest.getSmsCode());
//        verifySignRequest.setClbckUrl("");
        verifySignRequest.setIdNo(payerInfo.getCertId());
        verifySignRequest.setIdType(payerInfo.getCertType());
        verifySignRequest.setMobNo(payerInfo.getPhoneNo());
        verifySignRequest.setSgnAcctId(payerInfo.getAcctNo());
        verifySignRequest.setSgnAcctIssrId(payerInfo.getOpenBankNo());
        verifySignRequest.setSgnAcctNm(payerInfo.getAcctName());
        verifySignRequest.setSgnAcctTp(payerInfo.getAcctType());
        verifySignRequest.setChannelId("");
        verifySignRequest.setChannelType("");

        //调用通道发起签约
        DubboService dubboService = new DubboServiceImpl();
        Result<VerifySignResponse> verifySignResponseResult = (Result<VerifySignResponse>) dubboService.callDubboInnerMethod(NuccAcqPayServiceImpl.class, "verifySignQuick", verifySignRequest);
        if (!verifySignResponseResult.isSuccess()) {
            throw new BizException(verifySignResponseResult.getResult().getRespCode(), verifySignResponseResult.getResult().getRespDesc());
        }
        return intelligencePaymentSignVerifyResponse;
    }
}
